package com.audi.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
//import com.asus.audi.adapter.SQLiteAsusHelper;
import com.audi.BaseActivity;
import com.audi.adapter.RootAccessHomeMemberAdapter;
import com.audi.dslv.DragSortController;
import com.audi.dslv.DragSortListView;
import com.audi.dslv.DragSortListView.DropListener;
import com.audi.dto.MemberItem;

@SuppressLint("ClickableViewAccessibility")
public class RootAccessHomeMemberActivity extends BaseActivity {

	// declare view for action bar
	private TextView txtHomeMemberBack;

	// declare view for content view
	private Context mContext;
	private ArrayList<MemberItem> mRootAccessHomeMemberList;
	private ImageView imvMyMemberIcon;
	private ImageView imvMyMemberStatus;
	@SuppressWarnings("unused")
	private ImageView imvMyMemberMenu;
	private TextView txtMyMemberName;
	private TextView txtMyMemberEmail;
	private MemberItem mMyMember;

	private DragSortListView mRootAccessHomeMemberListView;
	private DragSortController mRootAccessHomeController;
	private RootAccessHomeMemberAdapter mRootAccessHomeMemberAdapter;

	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_root_access_home_member);
		initActionBar(R.layout.action_bar_home_member);
		setDisableDrawer();
		initView();
		initData();
		initListener();
	}

	public DropListener onDrop = new DropListener() {
		public void drop(int from, int to) {
			if (from != to) {
				MemberItem s = (MemberItem) mRootAccessHomeMemberList.get(from);
				mRootAccessHomeMemberList.remove(from);
				mRootAccessHomeMemberList.add(to, s);
				mRootAccessHomeMemberAdapter.notifyDataSetChanged();
			}
		}
	};

	private void initView() {
		// mapping view in action bar
		txtHomeMemberBack = (TextView) findViewById(R.id.txt_home_member_back);

		// mapping view in content view
		// mListViewMember = (ListView) findViewById(R.id.lv_member_list);
		imvMyMemberIcon = (ImageView) findViewById(R.id.imv_my_member_icon);
		imvMyMemberStatus = (ImageView) findViewById(R.id.imv_my_member_status);
		imvMyMemberMenu = (ImageView) findViewById(R.id.imv_my_member_menu);
		txtMyMemberName = (TextView) findViewById(R.id.txt_my_member_name);
		txtMyMemberEmail = (TextView) findViewById(R.id.txt_my_member_email);

		mRootAccessHomeMemberListView = (DragSortListView) findViewById(R.id.lv_member_list);
		mRootAccessHomeController = new DragSortController(
				mRootAccessHomeMemberListView);
		mRootAccessHomeController.setDragHandleId(R.id.rlt_drag);
		mRootAccessHomeController.setRemoveEnabled(false);
		mRootAccessHomeController.setSortEnabled(true);
		mRootAccessHomeController.setDragInitMode(2);
		mRootAccessHomeMemberListView
				.setFloatViewManager(mRootAccessHomeController);
		mRootAccessHomeMemberListView.setOnTouchListener(mRootAccessHomeController);
		mRootAccessHomeMemberListView.setDragEnabled(true);
		mRootAccessHomeMemberListView.setDropListener(onDrop);
	}

	private void initData() {
		// init general object
		mContext = RootAccessHomeMemberActivity.this;

//		new SQLiteAsusHelper(mContext);

		dumpMyMember();
		if (TextUtils.isEmpty(mMyMember.getMemberPhoto())) {
			imvMyMemberIcon.setImageResource(R.drawable.icon_member_status);
		} else {
			BitmapDrawable bitmapDrawable = new BitmapDrawable(
					mContext.getResources(), mMyMember.getMemberPhoto());
			imvMyMemberIcon.setImageDrawable(bitmapDrawable);
		}
		if (mMyMember.getMemberStatus() == 1) {
			imvMyMemberStatus.setImageResource(R.drawable.circle_at_home);
		} else {
			imvMyMemberStatus.setImageResource(R.drawable.circle_not_at_home);
		}
		txtMyMemberName.setText(mMyMember.getMemberName());
		txtMyMemberEmail.setText(mMyMember.getMemberEmail());
		dumpListMember();
	}

	private void initListener() {
		// set listener for acionbar
		txtHomeMemberBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Toast.makeText(mContext, "Back from Home Member Screen",
						Toast.LENGTH_SHORT).show();
			}
		});

	}

	private void dumpMyMember() {
		mMyMember = new MemberItem(0, "", "My Member", "mymember@gmail.com", 1, 0);
	}

	private void dumpListMember() {
		mRootAccessHomeMemberList = new ArrayList<MemberItem>();
		mRootAccessHomeMemberList.add(new MemberItem(0, "", "%user_name%",
				"member1@gmail.com", 1, 0));
		mRootAccessHomeMemberList.add(new MemberItem(0, "", "%user_name%",
				"member2@gmail.com", 0, 0));

		mRootAccessHomeMemberAdapter = new RootAccessHomeMemberAdapter(this,
				mRootAccessHomeMemberList);
		mRootAccessHomeMemberListView.setAdapter(mRootAccessHomeMemberAdapter);
	}

	@Override
	public void onRefreshData() {
		// TODO Auto-generated method stub
		
	}

}